<?php
/**
 * Organization model class file.
 *
 * @package   models
 * @since     1.0
 * @filesource
 */

/**
 * This is the model class for table "{{organization}}".
 * This is a DAO class which is used by DisorderController or other controllers
 * to operate data in database or return data in a specified format.
 *
 * The followings are the available columns in table '{{organization}}':
 * @property integer $id
 * @property string $name
 * @property string $contactPerson
 * @property string $contactNumber
 * @property string $address
 * @property string $website
 * @property string $modelRefNumber
 * @property string $amplifierBrandModel
 * @property string $adcBrandModel
 * @property string $licenseTerm
 * @property string $description
 * @property string $createTime
 * @property string $uploadFolder
 * @property integer $active
 *
 * The followings are the available model relations:
 * @property OrganizationAccess[] $organizationAccesses - array of OrganizationAccesses objects related to an organization.
 * @property PatientSignal[] $patientSignals - a array of PatientSignal objects related to an organization.
 */
class Organization extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Organization the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * Returns the table name related to this model class.
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{organization}}';
	}

	/**
	 * Returns the validation rules.
	 * The rules will be used when requesting validation by call self.validate().
	 * Many class method will call self.validate(), eg: CActiveRecord.save().
	 * @return array validation rules for model attributes. 
	 */
	public function rules()
	{
		return array(
			array('name, contactPerson, contactNumber, address, uploadFolder, active', 'required'),
			array('active', 'numerical', 'integerOnly'=>true),
			array('modelRefNumber, licenseTerm', 'default','value'=>''),
			array('name, contactPerson, contactNumber, website, modelRefNumber, amplifierBrandModel, adcBrandModel, uploadFolder', 'length', 'max'=>255),
			array('uploadFolder', 'match', 'pattern'=>'/^[0-9a-zA-z_]+$/','message'=>'{attribute} is invalid. Must be letters, digits and underscore only.'),
			array('description', 'safe','on'=>'insert,update'),
			// The following rule is used by search().
			array('name, contactPerson, active', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * Returns behaviors array
	 * @return array.
	 * @see CModel::behaviors()
	 */
	public function behaviors(){
		return array(
			// This setting will populate createTime variable automatically when 
			// creating an organization.
	  		'CTimestampBehavior' => array(
	  			'class' => 'zii.behaviors.CTimestampBehavior',
	  			'createAttribute' => 'createTime',
				'updateAttribute' => null,
		)
		);
	}
	
	
	/**
	 * Returns the declaration of named scopes.
	 * @return array
	 * @see CActiveRecord::scopes()
	 */
	public function scopes(){
		return array(
			'active'=>array(
		            'condition'=>'active=1',
			),
		);
	}


	/**
	 * Returns the relationship between this Model class and other Model class
	 * based on foreign key constraint between corresponding tables.
	 * The relationships involved in this application involves HAS_MANY, HAS_ONE, BELONGS_TO,
	 * please refer to Yii document for details.
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
			// one organization could be related to null, or many organization accesses.
			'organizationAccesses' => array(self::HAS_MANY, 'OrganizationAccess', 'organizationId'),
			// one organization could be related to null, or many patient signals.
			'patientSignals' => array(self::HAS_MANY, 'PatientSignal', 'organizationId'),
		);
	}

	/**
	 * Returns the labels for each column variables. 
	 * This will be used to render label on page.
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'contactPerson' => 'Contact Person',
			'contactNumber' => 'Contact Number',
			'address' => 'Address',
			'website' => 'Website',
			'modelRefNumber' => 'Model and Reference Number',
			'amplifierBrandModel' => 'Amplifier Brand Model',
			'adcBrandModel' => 'ADC Brand&Model',
			'licenseTerm' => 'License Term',
			'description' => 'Description',
			'createTime' => 'Create Time',
			'uploadFolder' => 'Upload Folder',
			'active' => 'Active',
		);
	}

	/**
	 * Delete the organization and also delete the signal records on the disk.
	 * This metod will rewrite the delete method inherited from CActiveRecord to delete
	 * patient signal records and all files store on server.
	 * @see CActiveRecord::delete()
	 */
	public function delete(){
		$signals=$this->patientSignals;
		foreach ($signals as $signal){
			$signal->delete();
		}
		parent::delete();
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions 
	 * declared in this function.
	 * @return CActiveDataProvider the data provider that can return the models 
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('name',$this->name,true);
		$criteria->compare('contactPerson',$this->contactPerson,true);
		$criteria->compare('contactNumber',$this->contactNumber,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('website',$this->website,true);
		$criteria->compare('modelRefNumber',$this->modelRefNumber,true);
		$criteria->compare('amplifierBrandModel',$this->amplifierBrandModel,true);
		$criteria->compare('adcBrandModel',$this->adcBrandModel,true);
		$criteria->compare('licenseTerm',$this->licenseTerm,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('createTime',$this->createTime,true);
		$criteria->compare('uploadFolder',$this->uploadFolder,true);
		$criteria->compare('active',$this->active);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Get the full upload folder path, this method will look up the root path from 
	 * configuration and contact with the upload folder of itself
	 * @return string the full path for the upload folder for the Organization
	 */
	public function getFullUploadFolder(){
		return GlobalConfiguration::model()->getByKey('UPLOAD_ROOT_FOLDER')
		.DIRECTORY_SEPARATOR.$this->uploadFolder;
	}
}